// pages/user/user.js
var base = require('../../base.js');  //路径可能做相应调整
const _ = base._; //翻译函数
const PAGES = require('../../utils/pages.js')
const { HTTP } = require('../../lib/http.js')
const { URL } = require('../../lib/urls.js')
const { getUserToken, getUserInfo, getIsLogin, saveUserInfoNoJump } = require('../../lib/user.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: [],
    userLeve:"",
    userMemberId:"",
    couponCount:0,
    daoqiTime:"",
    isEdit:false,
    ziyuanTabList:[],
    xuqiuTabList:[],
    ziyuanList:[],
    xuqiuList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      _t: base._t(), //翻译
    });
    if (!getIsLogin()){
      wx.navigateTo({
        url: PAGES.index,
      })
      return;
    }
    this.setData({
      userLeve: "VIP:" + getUserInfo().MemberLevel,
      userMemberId: getUserInfo().userId,
      daoqiTime: getUserInfo().HasVideoMember ? getUserInfo().VideoEndTime : _("暂未开通")
    });
    for(let i=0;i<5;i++){
      this.data.ziyuanTabList.push({'name':'资源'+i});
    }
    for(let i=0;i<7;i++){
      this.data.xuqiuTabList.push({'name':'需求'+i});
    }
    for(let i=0;i<3;i++){
      this.data.ziyuanList.push({'name':'请输入资源'});
    }
    for(let i=0;i<3;i++){
      this.data.xuqiuList.push({'name':'请输入需求'});
    }
    this.setData({
      ziyuanTabList:this.data.ziyuanTabList,
      xuqiuTabList:this.data.xuqiuTabList,
      ziyuanList:this.data.ziyuanList,
      xuqiuList:this.data.xuqiuList
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 4
      })
    }
    this.getCartCount();
    this.getNetUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 优惠券
   */
  gotoCouponTap: function () {
    wx.navigateTo({
      url: PAGES.myCoupon,
    })
  },
  // 我的订单
  gotoMyOrderTap: function () {
    wx.navigateTo({
      url: PAGES.myOrder,
    })
  },
  // 收支明细
  gotoBillTap: function () {
    wx.navigateTo({
      url: PAGES.billDetails,
    })
  },
  // 邀请码
  inviteCodeTap: function () {
    wx.navigateTo({
      url: PAGES.inviteCode,
    })
  },
  // 切换语言
  languageTap: function () {
    wx.navigateTo({
      url: PAGES.setting,
    })
  },
  //设置页面
  gotoSetting: function () {
    wx.navigateTo({
      url: PAGES.setting,
    })
  },
  //视频会员
  gotoPayVideoTap: function () {
    wx.navigateTo({
      url: PAGES.videoMember,
    })
  },
  getNetUserInfo: function () {
    var that=this;
    HTTP({
      url: URL.user.postUserInfo,
      userToken: getUserToken(),
      data: {},
      showLoading: true,
      success: (res) => {
        //保存用户信息
        saveUserInfoNoJump(res.Data);
        that.setData({
          couponCount: res.Data.CouponCount,
          daoqiTime: getUserInfo().HasVideoMember ? getUserInfo().VideoEndTime : _("暂未开通")
        })
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  //获取购物车商品数量
  getCartCount: function () {
    let that = this;
    HTTP({
      url: URL.goods.cartCount,
      userToken: getUserToken(),
      data: {
      },
      success: (ress) => {
        var res = ress.Data;
        console.log("hlw:" + res)
        var list = [];
        for (var i in that.getTabBar().data.list) {
          var model = that.getTabBar().data.list[i];
          if (i == 2) {
            model.tagNum = res;
          }
          list.push(model);
        }
        that.getTabBar().setData({
          list: list
        })
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  saveTab: function () {
    this.setData({
      isEdit:false,
    })
  },
  editNameTap: function () {
    this.setData({
      isEdit:true,
    })
  }
})